<template>
    <el-drawer
    :title="!dataForm.id ? '新增' : '修改'"
    direction="rtl"
    ref="drawer"
    size="40%"
    :visible.sync="visible">
    <div class="demo-drawer__header">
        <el-button size="mini" @click="visible = false">取 消</el-button>
        <el-button type="success" size="mini" @click="dataFormSubmit()">确定</el-button>
    </div>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
        <el-form-item label="配置类型" prop="type">
          <el-input v-model="dataForm.type" placeholder="配置类型"></el-input>
        </el-form-item>
        <el-form-item label="标识码" prop="code">
          <el-input v-model="dataForm.code" placeholder="标识码"></el-input>
        </el-form-item>
        <el-form-item label="显示名称" prop="name">
          <el-input v-model="dataForm.name" placeholder="显示名称"></el-input>
        </el-form-item>
        <el-form-item label="具体值" prop="value">
          <el-input v-model="dataForm.value" placeholder="具体值"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input v-model="dataForm.remark" placeholder="描述"></el-input>
        </el-form-item>
        <el-form-item label="排序号" prop="sort">
          <el-input v-model="dataForm.sort" placeholder="排序号"></el-input>
        </el-form-item>
        <el-form-item label="父节点ID" prop="parentId">
          <el-input v-model="dataForm.parentId" placeholder="父节点ID"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="dataForm.status" placeholder="请选择状态" style="width:100%">
              <el-option
                v-for="item in statuses"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        statuses:[
          {label:'无效', value:0},
          {label:'有效', value:1}
        ],
        dataForm: {
          id: 0,
          type: '',
          code: '',
          name: '',
          value: '',
          remark: '',
          sort: '',
          parentId: '',
          status: 1
        },
        dataRule: {
          type: [
            { required: true, message: '请填写配置类型', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请填写标识码', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请填写显示名称', trigger: 'blur' }
          ],
          value: [
            { required: true, message: '请填写具体值', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '请填写排序号', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '请选择状态', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/confInfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.type = data.confInfo.type
                this.dataForm.code = data.confInfo.code
                this.dataForm.name = data.confInfo.name
                this.dataForm.value = data.confInfo.value
                this.dataForm.remark = data.confInfo.remark
                this.dataForm.sort = data.confInfo.sort
                this.dataForm.parentId = data.confInfo.parentId
                this.dataForm.status = data.confInfo.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/confInfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'code': this.dataForm.code,
                'name': this.dataForm.name,
                'value': this.dataForm.value,
                'remark': this.dataForm.remark,
                'sort': this.dataForm.sort,
                'parentId': this.dataForm.parentId,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
    .demo-drawer {
        &__content {
            display: flex;
            flex-direction: column;
            height: 100%;
            form {
                flex: 1;
            }
        }
        &__footer {
            display: flex;
            button {
                flex: 1;
            }
        }
        &__header {
            text-align:right;
            margin-bottom:20px;
            width:100%;
        }
    }

    .el-drawer__body {
        padding: 20px;
    }

</style>
